# coding:utf-8
# 曾山
# 时间 2022/5/8 1:58
from flask_restful import Resource,reqparse,request
import json
from comment.models.index import *
from comment.utils.data2dict import datalist2dict,data2dict
from comment.utils.shopping_redis import redis_cenlit
#商品分类的资源类
class Shopping_Category(Resource):
    def get(self):
        #从redis数据库中读取数据
        data_cache = redis_cenlit.get("index_category")
        if data_cache:#如果redis中有数据 则返回redis中的数据
            return json.loads(data_cache)
        else:#如果redis中没有数据 则从mysql中查询数据 并保存到redis中
            # rq = reqparse.RequestParser()
            # rq.add_argument('parent_id', type=int, required=True)
            # req = rq.parse_args()

            data = self.getData(0)
            if data:
                for item in data:
                    item.update({'list': ''})
                    data_second = self.getData(item["id"])
                    item['list'] = data_second
                    for item1 in data_second:
                        item1.update({'list': ''})
                        data_third = self.getData(item1['id'])
                        item1['list'] = data_third
                #把查询到的数据写进redis数据库中
                redis_cenlit.setex('index_category',3600*24,json.dumps(data))
                return data
            else:
                return {'message': 'none'}



    @staticmethod
    def getData(parent_id):
        res = Category.query.with_entities(Category.id,Category.parent_id,Category.name)\
              .filter(Category.parent_id==parent_id).order_by(Category.sort.asc()).limit(10).all()

        if res:
            return datalist2dict(res)

        else:
            return 'none'

#新品推荐
class Shoping_HomeNewProduct(Resource):
    def get(self):
        data_cache = redis_cenlit.get('index_HomeNewProduct')
        if data_cache:
            return json.loads(data_cache)
        else:
            res = HomeNewProduct.query.join(Product,HomeNewProduct.product_id==Product.id)\
                  .with_entities(Product.id,Product.default_pic,Product.price,Product.product_name,Product.rel_category3_id)\
                  .order_by(HomeNewProduct.sort.asc()).limit(10).all()

            if res:
                data= datalist2dict(res)
                redis_cenlit.setex('index_HomeNewProduct',3600*24,json.dumps(data))
                return data
            else:
                return {'message':'none'}

#首页人气热搜
class Shopping_HomeRecommendProduct(Resource):
    def get(self):
        data_cache = redis_cenlit.get("index_HomeRecommendProduct")

        if data_cache:
            return json.loads(data_cache)
        else:
            res = HomeRecommendProduct.query.join(Product,HomeRecommendProduct.product_id==Product.id)\
                .with_entities(Product.id,Product.default_pic, Product.price,Product.product_name,Product.rel_category3_id)\
                .limit(10).all()

            if res:
                data =  datalist2dict(res)
                redis_cenlit.setex('index_HomeRecommendProduct',3600*24,json.dumps(data))
                return data
            else:
                return {'message':'none'}

class Shopping_RecommentSubject(Resource):
    def get(self):
        data_cache = redis_cenlit.get('index_RecommentSubject')
        if data_cache:
            return json.loads(data_cache)
        else:
            res = CmsSubject.query.filter(CmsSubject.show_status==1).all()
            if res:
                data = datalist2dict(res)
                for i in range(len(data)):
                     res_product = CmsSubjectProductRelation.query.join(Product,CmsSubjectProductRelation.product_id==Product.id)\
                        .filter(CmsSubjectProductRelation.subject_id==data[i]['id'])\
                        .with_entities(Product.id,Product.default_pic,Product.price,Product.product_name,Product.rel_category3_id)\
                        .limit(10).all()
                     if res_product:

                         data[i]['productList']=datalist2dict(res_product)
                     else:
                         data[i]['productList']=''
                redis_cenlit.setex('index_RecommentSubject',3600*24,json.dumps(data))
                return data
            else:
                return {'message':'none'}
        # def get(self):
        #     res = CmsSubject.query.filter(CmsSubject.show_status == 1).all()
        #     res_list = []
        #     for res_obj in res:
        #         if res_obj:
        #             data = data2dict(res_obj)
        #             res_product = CmsSubjectProductRelation.query.join \
        #                 (Product, CmsSubjectProductRelation.product_id == Product.id).filter \
        #                 (CmsSubjectProductRelation.subject_id == res_obj.id).with_entities \
        #                 (Product.id, Product.default_pic, Product.price, Product.product_name,
        #                  Product.rel_category3_id).limit(10).all()
        #             if res_product:
        #                 data_product = datalist2dict(res_product)
        #                 data['productList'] = data_product
        #                 res_list.append(data)
        #     if len(res_list) > 0:
        #         return res_list
        #     else:
        #         return {'message': 'none'}